package com.hzjy.api.controller;

import com.hzjy.api.beans.HzjyCollection;
import com.hzjy.api.service.BqTokenService;
import com.hzjy.api.service.impl.HzjyCollectionServiceImpl;
import com.hzjy.api.vo.BqLoginUser;
import com.hzjy.api.vo.BqUser;
import com.hzjy.common.core.controller.ApiBaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.utils.PageData;
import com.hzjy.common.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 我要挂牌
 *
 * @author
 */

@Api("我要收藏页")
@RestController
@RequestMapping("/api/mycollection")
public class MyCollectionController extends ApiBaseController {
    private static final Logger log = LoggerFactory.getLogger(MyCollectionController.class);
    @Autowired
    private BqTokenService bqTokenService;
    @Autowired
    private HzjyCollectionServiceImpl hzjyCollectionServiceImpl;

    /**
     * 请求参数 当前页currentPage，类型state(多种用,隔开)   返回：当前页currentPage，总页数totalPage，总条数totalResult 数据
     *
     * @param pd
     * @return
     */
    @ApiOperation("我的收藏列表")
    @PostMapping("/list")
    public AjaxResult myTradingList(@RequestBody PageData pd) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        BqUser bqUser = loginUser.getBqUser();
        Long userId = bqUser.getUserId();
        Object currentPage = pd.get("currentPage");
        if (currentPage == null) {
            return AjaxResult.error("参数错误");
        }
        Integer page = Integer.parseInt(currentPage.toString());
        pd.put("size", pd.get("size") == null || pd.getInteger("size") < 1 ? 10 : pd.get("size"));
        if (page < 1) {
            pd.put("currentPage", 0);
        } else {
            pd.put("currentPage", (page - 1) * pd.getInteger("size"));
        }
        pd.put("onwer", userId);
        Map map = hzjyCollectionServiceImpl.queryMyCollectionsPage(pd, userId);
        map.put("currentPage", page);
        map.put("currentUserId", userId);
        return AjaxResult.success("查询成功", map);
    }


    @ApiOperation("添加收藏")
    @PostMapping("/addMyTrading")
    public AjaxResult addMyTrading(@RequestBody PageData pd) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        BqUser bqUser = loginUser.getBqUser();
        Object productId = pd.get("productId");
        if (productId == null) {
            return AjaxResult.error("参数错误");
        }
        HzjyCollection hzjyCollection = new HzjyCollection();
        hzjyCollection.setProductId(productId.toString());
        hzjyCollection.setOwner(bqUser.getUserId().toString());
        List<HzjyCollection> queryList = hzjyCollectionServiceImpl.selectHzjyCollectionList(hzjyCollection);
        if (!CollectionUtils.isEmpty(queryList)) {
            return AjaxResult.error("当前作品已经收藏过.");
        }
        hzjyCollection.setCollectTime(new Date());
        hzjyCollectionServiceImpl.insertHzjyCollection(hzjyCollection);
        return AjaxResult.success("收藏成功");
    }

    @ApiOperation("取消收藏")
    @PostMapping("/cannelMyTrading")
    public AjaxResult cannelMyTrading(@RequestBody PageData pd) {
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        if (loginUser == null) {
            return AjaxResult.error(401, "未登录");
        }
        BqUser bqUser = loginUser.getBqUser();
        Object productId = pd.get("productId");
        if (productId == null) {
            return AjaxResult.error("参数错误");
        }
        HzjyCollection hzjyCollection = new HzjyCollection();
        hzjyCollection.setProductId(productId.toString());
        hzjyCollection.setOwner(bqUser.getUserId().toString());
        List<HzjyCollection> queryList = hzjyCollectionServiceImpl.selectHzjyCollectionList(hzjyCollection);
        if (CollectionUtils.isEmpty(queryList)) {
            return AjaxResult.error("当前作品未收藏,无法取消收藏.");
        }
        hzjyCollectionServiceImpl.deleteHzjyCollectionById(queryList.get(0).getId());
        return AjaxResult.success("取消收藏成功");
    }

}
